export const OTMixinBodyFooter = {
    methods: {
        footerMethod() {
            if (!this.table['show-footer']) {
                return
            }
            if (!this.table.footers) {
                this.table.footers = "合计"
            }
            // 获取原始列定义
            const originalColumns = getTableColumns(this.table.columns);
            // 初始化页脚数据数组，长度与列数相同，初始值都为 null
            const footerData = [];
            let titles = this.table.footers.split(",");
            for (let i = 0; i < titles.length; i++) {
                footerData.push(new Array(originalColumns.length).fill(null));
                // 遍历原始列定义，计算每个列对应的页脚数据
                originalColumns.forEach((column, _columnIndex) => {
                    if (_columnIndex === 0) {
                        footerData[i][_columnIndex] = titles[i];
                    } else if (column.totalToOtherProp) {
                        if (titles.length == 1 || i == 0) {
                            if (column.totalToOtherProp.length === 3 || column.totalToOtherProp.trim().indexOf("{") !== 0) {
                                this.doSetFooterData(column.field, footerData, i, _columnIndex, column.totalToOtherProp)
                            } else {
                                try {
                                    let operation = JSON.parse(column.totalToOtherProp);
                                    this.doSetFooterData(column.field, footerData, i, _columnIndex, operation[titles[i]])
                                } catch (e) {
                                    console.log(e, column.totalToOtherProp)
                                }
                            }
                        } else if (column.totalToOtherProp.length !== 3 && column.totalToOtherProp.trim().indexOf("{") === 0) {
                            try {
                                let operation = JSON.parse(column.totalToOtherProp);
                                this.doSetFooterData(column.field, footerData, i, _columnIndex, operation[titles[i]])
                            } catch (e) {
                                console.log(e, column.totalToOtherProp)
                            }
                        }
                    }
                });
            }
            return footerData;
        }, // 计算总和的函数
        sumNum(field) {
            return this.table.data.reduce((acc, item) => {
                const value = parseFloat(item[field]);
                return isNaN(value) ? acc : acc + value;
            }, 0);
        },
        doSetFooterData(field, footerData, i, _columnIndex, totalToOtherProp) {
            if(!totalToOtherProp){
                return
            }
            const operation = totalToOtherProp.toLowerCase();
            switch (operation) {
                case 'avg':
                    footerData[i][_columnIndex] = (this.sumNum(field) / this.table.data.length).toFixed(3);
                    break;
                case'sum':
                    footerData[i][_columnIndex] = this.sumNum(field).toFixed(3);
                    break;
                case'max':
                    footerData[i][_columnIndex] = this.maxOrMin(field, true).toFixed(3);
                    break;
                case'min':
                    footerData[i][_columnIndex] = this.maxOrMin(field, false).toFixed(3);
                    break;
                default:
                    try {
                        footerData[i][_columnIndex] = eval(totalToOtherProp)
                    } catch (e) {
                        console.log("表尾异常：", e)
                    }
                    break;
            }
        },
        //除数
        divisorNum(field, filed2) {
            if (this.table.data.length == 0) {
                return 0
            }
            return ((this.table.data.reduce((acc, item) => {
                const value = parseFloat(item[field]);
                return isNaN(value) ? acc : acc + value;
            }, 0) / this.table.data.reduce((acc, item) => {
                const value = parseFloat(item[filed2]);
                return isNaN(value) ? acc : acc + value;
            }, 0)) * 100).toFixed(3);
        },
        // 计算最大值或最小值的函数
        maxOrMin(field, isMax) {
            let result = isMax ? -Infinity : Infinity;
            this.table.data.forEach(item => {
                const value = parseFloat(item[field]);
                if (!isNaN(value)) {
                    if (isMax && value > result) {
                        result = value;
                    } else if (!isMax && value < result) {
                        result = value;
                    }
                }
            });
            return result;
        }
    }
}

function getTableColumns(columns) {
    // 分离固定在左侧、右侧和不固定的列
    const fixedLeftColumns = [];
    const nonFixedColumns = [];
    const fixedRightColumns = [];

    columns.forEach(column => {
        if (column.visible != false) {
            if (column.fixed === 'left') {
                fixedLeftColumns.push(column);
            } else if (column.fixed === 'right') {
                fixedRightColumns.push(column);
            } else {
                nonFixedColumns.push(column);
            }
        }
    });

// 对非固定列按照 sort 属性排序
    nonFixedColumns.sort((a, b) => {
        const sortA = a.sort || 0;
        const sortB = b.sort || 0;
        return sortA - sortB;
    });

// 合并排序后的列
    return [...fixedLeftColumns, ...nonFixedColumns, ...fixedRightColumns];
}

